<?php

namespace app\admin\model;

use think\Model;


class Price extends Model
{

    

    

    // 表名
    protected $name = 'price';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    protected static function init()
    {
        self::afterUpdate(function ($row) {
            //修改添加价格记录
            $log = db('price_log')
//                ->where('title',$row['title'])
                ->where('day',date('Y-m-d',time()))
                ->find();
            if($log){
                db('price_log')->where('id',$log['id'])->update([
                    'price' => $row['price'],
                    'pink_price' => $row['pink_price'],
                    'small_price' => $row['small_price']
                ]);
            }else{
                db('price_log')->insert([
//                    'title' => $row['title'],
                    'price' => $row['price'],
                    'pink_price' => $row['pink_price'],
                    'small_price' => $row['small_price'],
                    'day' => date('Y-m-d',time()),
                    'createtime' => time(),
                    'updatetime' => time()
                ]);
            }
        });
    }
    







}
